import tkinter
import time
import random
class Ball():
    def __init__(self,canvas,color):
        self.canvas = canvas
        self.id=canvas.create_oval(0,0,35,35,fill=color)
        canvas.move(self.id,395,250)
        level=[-9,-7,-5,-3,-1,1,3,5,7,9]
        self.x=random.choice(level)
        self.y=random.choice(level)
    def draw(self):
        l1=[-9,-7,-5,-3,-1]
        l2=[1,3,5,7,9]
        canvas.move(self.id,self.x,self.y)
        pos=canvas.coords(self.id)
        if pos[3]>=600:
            self.y=random.choice(l1)
        if pos[1]<=0:
            self.y=random.choice(l2)
        if pos[2]>=800:
            self.x=random.choice(l1)
        if pos[0]<=0:
            self.x=random.choice(l2)
tk = tkinter.Tk()
tk.title("弹球小游戏")
canvas=tkinter.Canvas(tk,width=800,height=600)
canvas.pack()
tk.update()

ball=Ball(canvas,"blue")
ball2=Ball(canvas,"red")
while 1:
    ball.draw()
    ball2.draw()
    tk.update_idletasks()
    tk.update()
    time.sleep(0.01)
